//
//  FinancialDetailsVC.m
//  phmall
//
//  Created by admin on 2017/11/13.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "CreditViewController.h"
#import "FinancialDetailsCell.h"
#import "MoneyRechargeVC.h"
#import "BuyPointsViewController.h"
#import "DetailViewController.h"
#import "CreditView.h"
#import "EvaluationDetailsVC.h"
#import "CreditModel.h"
@interface CreditViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *titles;
@property(nonatomic,strong)NSMutableArray *creditArray;
@property(nonatomic,strong)CreditView *credit;
@end

@implementation CreditViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"0.0";
    //    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor =  [UIColor whiteColor];
    self.back.backgroundColor=[UIColor orangeColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self refreshCredit];
    
    self.title=Localized(@"信用管理");
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(0).priorityHigh();
    }];
    
    
    UIView *head=[[UIView alloc]init];
    head.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    self.credit=[[CreditView alloc]init];
    [head addSubview:self.credit];
    [self.credit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.bottom.equalTo(self.credit.favorable.mas_bottom).offset(10);
        make.bottom.equalTo(head.mas_bottom).offset(-0).priorityHigh();
    }];
    
    //添加webview前计算一次高度
    CGFloat height = [head systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = head.bounds;
    frame.size.height = height;
    head.frame = frame;
    [self.tableview setTableHeaderView:head];
    [head layoutIfNeeded];
    // Do any additional setup after loading the view.
}
-(void)rightBarAction{
    
}

- (void)refreshCredit{
    
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"credit_management"];
    [httpTool ZBPostNetDataWith:urlstr withDic:nil andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[NSString stringWithFormat:@"%@",responseDict[@"ret"]] isEqualToString:@"200"]) {
            NSMutableArray *data=responseDict[@"data"];
            NSMutableDictionary *comment_data=data[0][@"comment_data"];
            CreditModel *model= [CreditModel yy_modelWithDictionary:comment_data[@"seller"]];
            self.credit.model=model;
        }

    } andFailure:^{
        
    }];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FinancialDetailsCell *cell=[FinancialDetailsCell theShareCellWithTableView:tableView];
    cell.opaque=YES;
    cell.layer.drawsAsynchronously=YES;
    cell.layer.rasterizationScale=[UIScreen mainScreen].scale;
    cell.title.text=self.titles[indexPath.section];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed=YES;
    EvaluationDetailsVC *details = [[EvaluationDetailsVC alloc] init];
    details.items=indexPath.section;
    NSLog(@"%ld",(long)indexPath.section);
    [self.navigationController pushViewController:details animated:YES];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//MARK:-GETTER
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.showsVerticalScrollIndicator=NO;
        //        _myTableview.emptyDataSetSource=self;
        //        _myTableview.emptyDataSetDelegate=self;
        _tableview.scrollEnabled = YES;
    }
    return _tableview;
}
-(NSMutableArray *)titles{
    if (!_titles) {
        _titles=@[Localized(@"来自买家的评价"), Localized(@"来自卖家的评价")].copy;
    }
    return _titles;
}

-(NSMutableArray *)creditArray{
    if (!_creditArray) {
        _creditArray=[[NSMutableArray alloc]init];
    }
    return _creditArray;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

